﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Core.Graphics;

namespace WorldEditor.Tiles
{
    public partial class TileGrouping : UserControl
    {
        PalletForm pf;
        PickATile patCurrent;

        public TileGrouping()
        {
            InitializeComponent();

            pf = new PalletForm();
            pf.SelectedTileChanged += new EventHandler(pf_SelectedTileChanged);
        }

        void pf_SelectedTileChanged(object sender, EventArgs e)
        {
            if (patCurrent != null)
                patCurrent.Tile = (TileSetTile)sender;
        }

        private void tile_TileClicked(object sender, EventArgs e)
        {
            patCurrent = (PickATile)sender;

            pf.Show();
        }

        #region UpdateOtherTiles
        private void tilePlain_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tilePlain.Tile;
            //Update all tiles
            tilePlain1.Tile = tst;
            tilePlain2.Tile = tst;
            tilePlain3.Tile = tst;
            tilePlain4.Tile = tst;
            tilePlain5.Tile = tst;
            tilePlain6.Tile = tst;
            tilePlain7.Tile = tst;
            tilePlain8.Tile = tst;
        }

        private void tileUP_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileUP.Tile;
            //Update all tiles
            tileUp1.Tile = tst;
            tileUp2.Tile = tst;
        }

        private void tileDown_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileDown.Tile;
            //Update all tiles
            tileDown1.Tile = tst;
            tileDown2.Tile = tst;
        }

        private void tileRight_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileRight.Tile;
            //Update all tiles
            tileRight1.Tile = tst;
            tileRight2.Tile = tst;
        }

        private void tileLeft_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileLeft.Tile;
            //Update all tiles
            tileLeft1.Tile = tst;
            tileLeft2.Tile = tst;
        }

        private void tileUpRight_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileUpRight.Tile;
            //Update all tiles
            tileUpRight1.Tile = tst;
        }

        private void tileUpLeft_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileUpLeft.Tile;
            //Update all tiles
            tileUpLeft1.Tile = tst;
        }

        private void tileDownRight_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileDownRight.Tile;
            //Update all tiles
            tileDownRight1.Tile = tst;
        }

        private void tileDownLeft_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile tst = tileDownLeft.Tile;
            //Update all tiles
            tileDownLeft1.Tile = tst;
        }
        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            AutoTileInfo ati = new AutoTileInfo()
            {
                Plain = tilePlain.Tile.Tile,
                Up = tileUP.Tile.Tile,
                Down = tileDown.Tile.Tile,
                Left = tileLeft.Tile.Tile,
                Right = tileRight.Tile.Tile,
                UpRight = tileUpRight.Tile.Tile,
                UpLeft = tileUpLeft.Tile.Tile,
                DownLeft = tileDownLeft.Tile.Tile,
                DownRight = tileDownRight.Tile.Tile,
                CornerUpRight = tileCornerUpRight.Tile.Tile,
                CornerUpLeft = tileCornerUpLeft.Tile.Tile,
                CornerDownRight = tileCornerDownRight.Tile.Tile,
                CornerDownLeft = tileCornerDownLeft.Tile.Tile,
                Name = txtName.Text
            };

            AutoTileInfoCollection.GlobalAutoTileSets.Add(ati);

            ParentForm.Close();
        }
    }
}
